#include "TextureHandler.h"
//#include "tgaload.h"
#include "glext.h"

//#include "algebra3.h"
//#include "c_image.h"
#include "floatimage.h"

TextureHandler::TextureHandler(void)
{
}

TextureHandler::~TextureHandler(void)
{
}
int TextureHandler::Load_disp(vector<char*> filenames){
	return 1;
}
int TextureHandler::Load(char *filename, int texType)
{
	//puts("!!fuck!!Load it plz!!");
	
	//image_t   tmp;
	m_filename.append(filename);
	m_ext.append(filename+strlen(filename)-3);

	glGenTextures(1, &ogl_id);
	if(strcmp(m_ext.c_str(), "pfm") == 0)
	{

		floatimage img;
		//c_image img;
		if(img.LoadPFM(filename))
		{

			
			float* f = (float*)malloc(3 * img.sizex*img.sizey*sizeof(float));
			for(int x=0; x<img.sizex; x++)
				for(int y=0; y<img.sizey; y++){
					f[3*(img.sizey * x + y) + 0] = img.line(img.sizex-x-1)[y].r;
					f[3*(img.sizey * x + y) + 1] = img.line(img.sizex-x-1)[y].g;
					f[3*(img.sizey * x + y) + 2] = img.line(img.sizex-x-1)[y].b;
			
			}

			glBindTexture(GL_TEXTURE_2D, ogl_id);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
			
			
			//glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR );
			//glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR );
			
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
			glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
			//GL_RGBA16F_ARB 0x881A
			//GL_RGBA32F_ARB 0x8814
			//GL_RGBA
			//glTexImage2D(GL_TEXTURE_2D, 0, 0x881A, img.sizex, img.sizey, 0, GL_LUMINANCE, GL_FLOAT, f);
			glTexImage2D(GL_TEXTURE_2D, 0, 0x881A, img.sizex, img.sizey, 0, GL_RGB, GL_FLOAT, img.buf);
			//gluBuild2DMipmaps( GL_TEXTURE_2D, 3, img.width, img.height, GL_RGB, GL_UNSIGNED_BYTE, img.data );
			
			//gluBuild2DMipmaps( GL_TEXTURE_2D, 3, img.sizex, img.sizey, GL_RGB, GL_FLOAT, img.buf );

			img_width=img.sizex;
			img_height=img.sizey;

			free(f);
			printf("Done Loading: %s\n", filename);
			//delete [] img.buf;
			//glBindTexture(GL_TEXTURE_2D, 0);
			return 1;
		}
	}
	else if(strcmp(m_ext.c_str(), "bmp") == 0)
	{
		//printf("this is bmp %s\n", filename);
		//c_image img;
		floatimage img;
		if(img.LoadBMP(filename, 1.0))
		{

			float* f = (float*)malloc(3 * img.sizex*img.sizey*sizeof(float));
			for(int x=0; x<img.sizex; x++)
				for(int y=0; y<img.sizey; y++){
					f[3*(img.sizey * x + y) + 0] = img.line(img.sizex-x-1)[y].r;
					f[3*(img.sizey * x + y) + 1] = img.line(img.sizex-x-1)[y].g;
					f[3*(img.sizey * x + y) + 2] = img.line(img.sizex-x-1)[y].b;			
			}

			glBindTexture(GL_TEXTURE_2D, this->ogl_id);
			
#if 1		
			int filterType;

			if (texType==0)
			{
				filterType=GL_LINEAR;
			}else if (texType==1)
			{
				filterType=GL_NEAREST;
			}

			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filterType);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filterType);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
			//glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, img.sizex, img.sizey, 0, GL_RGB, GL_FLOAT, img.buf);
			glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
			
			glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, img.sizex, img.sizey, 0, GL_RGB, GL_FLOAT, img.buf);
			//glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, img.sizex, img.sizey, 0, GL_RGB, GL_UNSIGNED_BYTE, f);
			//glTexImage2D(GL_TEXTURE_2D, 0, 0x881A, img.width, img.height, 0, GL_RGB, GL_UNSIGNED_BYTE, img.data);
			
#else
			glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR );
			glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR );
			
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
			gluBuild2DMipmaps( GL_TEXTURE_2D, 3, img.sizex, img.sizey, GL_RGB, GL_FLOAT, img.buf );
#endif

			img_width=img.sizex;
			img_height=img.sizey;

			free(f);

			printf("Done Loading: %s\n", filename);
			//if(img.data)
			//delete [] img.data;
			////img.data=NULL;
			//glBindTexture(GL_TEXTURE_2D, 0);
			return 1;
		}else{
			printf("error loading:\n");
		}
		
	}
	else
	{
		printf("false : Load - texture format unsupported\n");
		return 0;
	}
}